﻿using System;
using System.Diagnostics.Contracts;

namespace OpenLS.Spreadsheet.UI.Internals
{
    class WorksheetProtectionViewModel
    {
        private readonly Worksheet _worksheet;

        public WorksheetProtectionViewModel(Worksheet worksheet)
        {
            Contract.Requires(worksheet != null);
            _worksheet = worksheet;
            SelectLockedCells = true;
            SelectUnlockedCells = true;
        }
        public bool CanUnprotect
        {
            get
            {
                if (_worksheet.Protection == null)
                    return false;
                return _worksheet.Protection.IsValidPassword(Password);
            }
        }

        public bool SelectLockedCells { get; set; }
        public bool SelectUnlockedCells { get; set; }
        public bool FormatCells { get; set; }
        public bool FormatColumns { get; set; }
        public bool FormatRows { get; set; }
        public bool InsertColumns { get; set; }
        public bool InsertRows { get; set; }
        public bool InsertHyperLink { get; set; }
        public bool DeleteColumns { get; set; }
        public bool DeleteRows { get; set; }
        public bool Sort { get; set; }
        public bool UseAutoFilter { get; set; } 
        public bool UsePivotTableReports { get; set; }
        public bool EditObjects { get; set; }
        public bool EditScenarios { get; set; }

        public string Password { get; set; }

        public void Unprotect()
        {
            _worksheet.Protection = null;

        }
        public bool HasProtection { get { return _worksheet.Protection != null; } }


        public void Protect()
        {
            _worksheet.Protection = new SheetProtection();
            _worksheet.Protection.SetPasswordHash(Password);
            _worksheet.Protection.SelectLockedCells = this.SelectLockedCells;
            _worksheet.Protection.SelectUnlockedCells = this.SelectUnlockedCells;
            _worksheet.Protection.FormatCells = FormatCells;
            _worksheet.Protection.FormatColumns = FormatColumns;
            _worksheet.Protection.FormatRows = FormatRows;
            _worksheet.Protection.DeleteRows = DeleteRows;
            _worksheet.Protection.DeleteColumns = DeleteColumns;
            _worksheet.Protection.Sort = Sort;
            _worksheet.Protection.Scenarios = EditScenarios;
            _worksheet.Protection.Objects = EditObjects;
            _worksheet.Protection.PivotTables = UsePivotTableReports;
            _worksheet.Protection.AutoFilter = this.UseAutoFilter;
            _worksheet.Protection.InsertColumns = this.InsertColumns;
            _worksheet.Protection.InsertRows = this.InsertRows;
            _worksheet.Protection.InsertHyperlinks = this.InsertHyperLink;
        }
    }
}